/**
 * @title: ParamFlowRuleMysqlConverter
 * @projectName mscloud
 * @Description TODO
 * @Author
 * @Date 2022/2/23 17:02
 */
package com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.convert.impl;

import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelParamFlowRuleDTO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.sentinel.convert.PullConverter;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;
@Data
@Component
public class ParamFlowRuleMysqlConverter implements PullConverter<List<SentinelParamFlowRuleDTO>,List<ParamFlowRule>> {
    private long recommendRefreshMs=10000;
    @Override
    public List<ParamFlowRule> convert(List<SentinelParamFlowRuleDTO> sources) {
        return sources.stream().map(
                item->{
                    ParamFlowRule paramFlowRule=new ParamFlowRule();
                    //JSONObject jsonObject = JSON.parseObject(item);
                    paramFlowRule.setBurstCount(item.getBurstCount());
                    //ParamFlowClusterConfig clusterConfig=jsonObject.getObject("clusterConfig",ParamFlowClusterConfig.class);
                    ParamFlowClusterConfig clusterConfig = item.getClusterConfig();
                    if(clusterConfig!=null){
                        paramFlowRule.setClusterConfig(clusterConfig);
                    }
                    paramFlowRule.setClusterMode(item.getClusterMode());
                    paramFlowRule.setControlBehavior(item.getControlBehavior());
                    paramFlowRule.setCount(item.getCount());
                    paramFlowRule.setDurationInSec(item.getDurationInSec());
                    paramFlowRule.setGrade(item.getGrade());
                    paramFlowRule.setMaxQueueingTimeMs(item.getMaxQueueingTimeMs());
                    //JSONArray paramFlowItemList = jsonObject.getJSONArray("paramFlowItemList");
                    List<ParamFlowItem> paramFlowItemList = item.getParamFlowItemList();
                    if(paramFlowItemList!=null){
                        paramFlowRule.setParamFlowItemList(paramFlowItemList);
                    }
                    paramFlowRule.setParamIdx(item.getParamIdx());
                    paramFlowRule.setControlBehavior(item.getControlBehavior());
                    paramFlowRule.setLimitApp(item.getLimitApp());
                    paramFlowRule.setResource(item.getResource());
                    return paramFlowRule;
                }
        ).collect(Collectors.toList());
    }
}
